package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.domain.AppRousePlanDetail;
import com.lebang.domain.TaskRouse;
import com.lebang.example.AppRousePlanDetailExample;
import com.lebang.example.DownloadMemberRecordExample;
import com.lebang.service.NewRouseService;
import com.lebang.service.earn.AppRousePlanDetailService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户签到数据更改
 *
 * @author hwq
 * @date 2020-03-25
 */
@Controller
@RequestMapping("/sys/approuseplandetail")
public class AppRousePlanDetailController {
    @Autowired
    private AppRousePlanDetailService baseService;
    @Autowired
    private NewRouseService newRouseService;

    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;

    @GetMapping("/list")
    public String list(Model model,String key) {
        model.addAttribute("key",key);
        return "/approuseplandetail/list";
    }


    @Login
    @PostMapping("/data")
    @ResponseBody
    public Map<String, Object> datalist(HttpSession session, Long page, Integer limit,String planKey) {
        AppRousePlanDetailExample example = new AppRousePlanDetailExample();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        AppRousePlanDetailExample.Criteria criteria = example.createCriteria();
        if(newRouseService.isRoot(session)){
            criteria.andSdkKeyNotIn(sdkList);
        }
        if(!TextUtil.isEmpty(planKey)){
            criteria.andPlanKeyEqualTo(planKey);
        }

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", baseService.selectByExample(example));
        returnMap.put("count", baseService.countByExample(example));
        returnMap.put("code", 0);
        return returnMap;

    }

    @GetMapping("/add")
    public String add(Integer id, Model model,String planKey,HttpSession session) {
        AppRousePlanDetail vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppRousePlanDetail();
            vo.setPlanKey(planKey);
            vo.setSdkKey("CSJ");
        }
        model.addAttribute("item", vo);
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("list", rouses);
        return "/approuseplandetail/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(AppRousePlanDetail vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        baseService.initData(vo.getPlanKey());
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        AppRousePlanDetail vo=baseService.selectById(id);
        baseService.deleteById(id);
        baseService.initData(vo.getPlanKey());
        return Callback.success("success", "删除成功");
    }

}
